import Vue from 'vue'
import vueRouter from 'vue-router'
import store from '../store/index.js'


import ordersRoutes from './orders.js'
import userRoutes from './user.js'
import systemRoutes from './system.js'
import authRoutes from './auth.js'
import goodsRoutes from './goods.js'

Vue.use(vueRouter);
var router = new vueRouter({
  routes: ordersRoutes.concat(userRoutes,systemRoutes,goodsRoutes,authRoutes)
});

// router.redirect({
//   // '*': '/user'
// })
// 添加路由钩子
router.beforeEach((to, from, next) => {
  if (to.matched.some(r => {
        return r.meta.requireAuth === false;
      })) {
      next();
    } else {
      // console.log(store.getters.isLogin);
      if (store.getters.isLogin) {
        next();
      } else {
        next({
          path: '/login',
          query: {
            redirect: to.fullPath
          }
        })
      }
    }
});

router.afterEach(route => {
  // this.$route;
  // console.log(router);
});

export default router;
